package cn.dt.basic.controller;

import cn.dt.basic.dto.RegisterDto;
import cn.dt.basic.exception.BusinessException;
import cn.dt.basic.util.AjaxResult;
import cn.dt.basic.service.IVerifycodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/verifyCode")
public class VerifycodeController {
    @Autowired
    private IVerifycodeService verifycodeService;


    @PostMapping("/smsCode")
    public  AjaxResult smsCode(@RequestBody RegisterDto dto){
        try {
            verifycodeService.smsCode(dto);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }
    @PostMapping("/semsCode")
    public  AjaxResult semsCode(@RequestBody RegisterDto dto){
        try {
            verifycodeService.semsCode(dto);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }

    /**
     *
     * @param key 是前端的发送过来的uuid
     * @return
     */
    @GetMapping("/image/{imageCodeKey}")
    public AjaxResult imagecode(@PathVariable("imageCodeKey") String key){
        try {
            String base64 = verifycodeService.imageCode(key);
            return AjaxResult.me().setResultObj(base64);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }
}
